import { Loading } from "element-ui";
import axios from "axios";

function isEmpty(obj) {
    return Object.keys(obj).length === 0;
}

function paramify(obj) {
    const params = Object.entries(obj)
        .map(item => {
            const key = encodeURIComponent(item[0]);
            const val = encodeURIComponent(typeof item[1] === "object" ? JSON.stringify(item[1]) : item[1]);
            return `${key}=${val}`;
        })
        .join("&");
    return `?${params}`;
}

export function assignOptions(src) {
    const srcOptions = typeof src === "boolean" ? { loading: src } : src;

    return Object.assign(
        {
            body: {},
            timeout: 5000,
            loading: true,
            loadingOptions: {
                background: "rgba(247, 247, 247, 0.5)",
            },
        },
        srcOptions,
    );
}

export async function post(url, options) {
    let load = null;
    const opts = assignOptions(options);

    if (opts.loading) {
        load = Loading.service(opts.loadingOptions);
    }

    // const res = await fetch(url, {
    //     method: "post",
    //     body: JSON.stringify(opts.body),
    // }).then(r => r.json());

    const res = await axios
        .post(url, opts.body, {
            timeout: opts.timeout,
        })
        .then(r => r.data)
        .catch(error => {
            /* eslint-disable no-console  */
            console.log(error);
            return {
                error: 500,
                message: "请求超时，服务器错误！",
            };
        });

    if (load) {
        load.close();
    }

    return res;
}

export async function get(url, options) {
    let load = null;
    const opts = assignOptions(options);

    if (opts.loading) {
        load = Loading.service(opts.loadingOptions);
    }

    if (!isEmpty(opts.body)) {
        url += paramify(opts.body);
    }

    // const res = await fetch(url).then(r => r.json());

    const res = await axios
        .get(url, {
            timeout: opts.timeout,
        })
        .then(r => r.data)
        .catch(error => {
            /* eslint-disable no-console  */
            console.log(error);
            return {
                error: 500,
                message: "请求超时，服务器错误！",
            };
        });

    if (load) {
        load.close();
    }

    return res;
}

export async function delet(url, options) {
    let load = null;
    const opts = assignOptions(options);

    if (opts.loading) {
        load = Loading.service(opts.loadingOptions);
    }

    if (!isEmpty(opts.body)) {
        url += paramify(opts.body);
    }

    // const res = await fetch(url).then(r => r.json());

    const res = await axios
        .delete(url, {
            timeout: opts.timeout,
        })
        .then(r => r.data)
        .catch(error => {
            /* eslint-disable no-console  */
            console.log(error);
            return {
                error: 500,
                message: "请求超时，服务器错误！",
            };
        });

    if (load) {
        load.close();
    }

    return res;
}
